<?php
namespace app\index\controller;

use app\index\model\Category;
use app\index\model\Channel;
use app\index\model\Courses;
use think\Request;
use think\Session;

class Index extends Common
{
    public function index(Request $request)
    {
        $where = [];
        $order = 'id desc';
        if ($request->param('classifyid')) {
            $classifyid = $request->param('classifyid', 0, 'intval');
            $where['classify_id'] = $classifyid;
            $this->assign('classifyid', $classifyid);
        }
        if ($request->param('search_title', '', 'filterString')) {
            $title = $request->param('search_title', '', 'filterString');
            $where['search_title'] = ['like', '%'.$title.'%'];
            $this->assign('title', $title);
        }
        //排序
        if ($request->param('order')) {
            if ($request->param('order', 0, 'intval') == 2) {
                $order = 'cps_generalize_earning desc';
            }
            $this->assign('order', $request->param('order'));
        }
        $categoryModel = new Category();
        $coursesModel = new Courses();
        $data = $coursesModel->getAllLists($where, $order);
        $classifys = $categoryModel->getAllLists();
        $this->assign('header_name', '单条内容推广列表');
        $this->assign('classifys', $classifys);
        $this->assign('lists', $data['lists']);
        $this->assign('page', $data['page']);
        return $this->fetch();
    }

    //生成单内容推广链接
    public function create_content(Request $request)
    {
        $course_id = $request->param('course_id', 0, 'intval');
        if (!$course_id) {
            $this->error('参数错误');
        }
        $coursesModel = new Courses();
        $course_info = $coursesModel->getInfoById($course_id);
        if (!$course_info) {
            trace(Session::get('cps_user_id').'课程查找失败:'.$coursesModel->getLastSql(), 'business');
            $this->error('未查找到课程');
        }
        $channelModel = new Channel();
        $channel_lists = $channelModel->getAllLists([
            'course_id' => $course_id,
            'user_id' => Session::get('cps_user_id')
        ], 'id asc', false);
        $this->assign('info', $course_info);
        $this->assign('header_name', '推广内容');
        $this->assign('channel_lists', $channel_lists);
        return $this->fetch();
    }

    //去生成渠道
    public function to_create_content(Request $request)
    {
        if ($request->isPost()) {
            $course_id = $request->param('course_id', 0, 'intval');
            if (!$course_id) {
                $this->error('参数错误');
            }
            $coursesModel = new Courses();
            $course_info = $coursesModel->getInfoById($course_id);
            if (!$course_info) {
                trace(Session::get('cps_user_id').'课程查找失败:'.$coursesModel->getLastSql(), 'business');
                $this->error('未查找到课程');
            }
            $channelModel = new Channel();
            $channel_id = $channelModel->createData($course_id);
            if ($channel_id) {
                $course_info['channel_id'] = $channel_id;
                $course_info['channel_url'] = $request->domain() . "/api/skip?channel_id={$channel_id}";
                $this->success('生成成功', '', $course_info);
            } else {
                trace(Session::get('cps_user_id').'生成推广渠道失败:'.$channelModel->getLastSql(), 'business');
                $this->error('生成失败,请稍后重试');
            }
        }
    }

    //下载图片
    public function download(Request $request)
    {
        $course_id = $request->param('course_id', 0, 'intval');
        if (!$course_id) {
            $this->error('参数错误');
        }
        $coursesModel = new Courses();
        $course_info = $coursesModel->getInfoById($course_id);
        if (!$course_info) {
            $this->error('未查找到课程');
        }
        if ($course_info['banner']) {
            preg_match('/.+\/(.+?)$/', $course_info['banner'], $matchs);
            $info = getimagesize($course_info['banner']);
            header("content-type:".$info['mime']);
            header("content-disposition:attachment;filename={$matchs[1]}");
            //header("content-length:".filesize($course_info['banner']));
            echo file_get_contents($course_info['banner']);
        }
    }
}
